﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.Globalization;

namespace Flitter.Twitter
{
    [JsonObject()]
    class SearchTimeline
    {
        [JsonProperty("results")]
        public List<Result> results { get; set; }

        public string refresh_url { get; set; }

        public string next_page { get; set; }

        public string query { get; set; }

        public Statuses toStatuses()
        {
            Statuses retval = new Statuses();

            List<StatusesStatus> statuses = new List<StatusesStatus>();
            foreach (Result r in this.results)
            {
                StatusesStatus st = new StatusesStatus();
                st.created_at = DateTime.ParseExact(r.created_at, "ddd, dd MMM yyyy HH:mm:ss zzzzz", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal).ToString("ddd MMM dd HH:mm:ss zzzzz yyyy", CultureInfo.InvariantCulture);
                st.id = r.id;
                st.text = r.text;

                st.user = new StatusesStatusUser();
                st.user.id = r.from_user_id;
                st.user.name = r.from_user;
                st.user.screen_name = r.from_user;
                st.user.profile_image_url = r.profile_image_url;

                statuses.Add(st);
            }

            retval.Items = statuses.ToArray();
            return retval;
        }

        public users toUsers()
        {
            users retval = new users();

            List<usersUser> users = new List<usersUser>();
            foreach (Result r in this.results)
            {
                usersUser uu = users.Find(delegate(usersUser current)
                {
                    return current.id == r.id;
                });

                if (uu == null)
                {
                    uu = new usersUser();
                    users.Add(uu);
                }

                uu.id = r.from_user_id;
                uu.name = r.from_user;
                uu.profile_image_url = r.profile_image_url;
                uu.screen_name = r.from_user_id;

                usersUserStatus uus = new usersUserStatus();
                uus.created_at = DateTime.ParseExact(r.created_at, "ddd, dd MMM yyyy HH:mm:ss zzzzz", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal).ToString("ddd MMM dd HH:mm:ss zzzzz yyyy", CultureInfo.InvariantCulture);
                uus.id = r.id;
                uus.text = r.text;

                //ちょっと無駄だけど、まあいいか。
                List<usersUserStatus> temp = new List<usersUserStatus>(uu.status);
                temp.Add(uus);
                uu.status = temp.ToArray();
            }

            return retval;
        }
    }

    [JsonArray()]
    class Result
    {
        public string id { get; set; }

        public string profile_image_url { get; set; }

        public string created_at { get; set; }

        public string from_user { get; set; }

        public string text { get; set; }

        public string from_user_id { get; set; }

    }
}
